local kaiqi = fk.CreateSkill{
  name = "kpaI__kaiqi"
}

kaiqi:addEffect(fk.TargetSpecifying, {
  max_phase_use_time = 2,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(kaiqi.name) and player.phase == Player.Play and
      data.firstTarget and self:withinTimesLimit(player, Player.HistoryPhase) and
      (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and
      not table.find(player.room:getAlivePlayers(false), function(p) return p.dying end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getCardIds("h"), function(cid)
      local card = Fk:getCardById(cid)
      return not card.is_passive and (card.type == Card.TypeBasic or card:isCommonTrick())
    end)
    local toshow = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      pattern = tostring(Exppattern:new{ id = cards }),
      skill_name = kaiqi.name,
      prompt = "#kpaI__kaiqi-invoke:::" .. data.card:toLogString()
    })
    if #toshow ~= 0 then
      event:setCostData(self, { cards = toshow })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tocard = Fk:getCardById(event:getCostData(self).cards[1])
    player:showCards(tocard)
    if player.dead then return end
    local choice_effect = "#kpaI__kaiqi_effect:::" .. tocard:toLogString()
    local choice_target = "#kpaI__kaiqi_target:::" .. tocard:toLogString()
    local choice_both = "#kpaI__kaiqi_both_pure"
    local suits = {}
    for _, card in ipairs({ data.card, tocard }) do
      if card.suit ~= Card.NoSuit then
        table.insertIfNeed(suits, card.suit)
      end
    end
    if #suits ~= 0 then
      choice_both = "#kpaI__kaiqi_both:::" .. table.concat(table.map(suits, function(suit)
      return Fk:translate(Card.getSuitString({ suit = suit }, true))
    end), "/")
    end
    local choices = { choice_effect, choice_target, choice_both }
    local choice = { room:askToChoice(player, {
      choices = choices,
      skill_name = kaiqi.name,
    }) }
    if choice[1] == choice_both then
      for _, symbol in ipairs(suits) do
        room:addTableMarkIfNeed(player, "@[suits]kpaI__kaiqi-round", symbol)
      end
      choice = { choice_effect, choice_target }
    end
    for _, choicess in ipairs(choice) do
      if choicess == choice_effect then --- 有前人经验，大胆开改
        local card = data.card:clone()
        local c = table.simpleClone(data.card)
        for k, v in pairs(c) do
          card[k] = v
        end
        card.skill = tocard.skill
        data.card = card
        data.use.card = card
        room:sendLog { type = "#placeholder_changeEffect", from = player.id,
          arg = kaiqi.name, arg2 = data.card:toLogString(), arg3 = tocard.name }
      elseif choicess == choice_target then
        local targets = table.filter(room.alive_players, function (p)
          return not target:isProhibited(p, tocard) and tocard.skill:modTargetFilter(p, target, {}, tocard, {bypass_distances = true})
        end)
        if #targets ~= 0 then
          local tos = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = tocard.skill:getMaxTargetNum(player, tocard),
            skill_name = kaiqi.name,
            cancelable = false,
            no_indicate = true,
            prompt = "#kpaI__kaiqi-target:::"..data.card:toLogString() .. ":" .. tocard:toLogString(),
          })
          room:sortByAction(tos)
          data:cancelAllTarget()
          for _, to in ipairs(tos) do
            data:addTarget(to)
          end
        end
      end
    end
  end
})
kaiqi:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    return card and player and table.contains(player:getTableMark("@[suits]kpaI__kaiqi-round"), card.suit)
  end
})

Fk:loadTranslationTable{
  ["kpaI__kaiqi"] = "开奇",
  [":kpaI__kaiqi"] = "出牌阶段限两次，当你使用即时牌指定目标时，若没有角色处于濒死状态，你可以展示一张有目标即时牌，然后可以令使用牌：<br>"..
  "1.效果改为展示牌；<br>"..
  "2.目标角色改为展示牌的合法目标。<br>"..
  "背水：本轮你不能使用使用牌和展示牌花色的牌。",

  ["#kpaI__kaiqi-invoke"] = "开奇：你可以展示一张即时牌，修改 %arg 的效果或目标。",
  ["#kpaI__kaiqi-choose"] = "开奇：请选择对 %dest 使用【%arg】的副目标",
  ["#kpaI__kaiqi-target"] = "开奇：请重新选择 %arg 的目标（基于 %arg2）",

  ["#kpaI__kaiqi_effect"] = "效果改为 %arg",
  ["#kpaI__kaiqi_target"] = "目标改为 %arg 的合法目标",
  ["#kpaI__kaiqi_both"] = "背水：本轮你不能使用%arg牌。",
  ["#kpaI__kaiqi_both_pure"] = "背水！",

  ["#placeholder_changeEffect"] = "由于 %arg 的效果，%from 使用的 %arg2 效果改为【%arg3】",

  ["@[suits]kpaI__kaiqi-round"] = "开奇",

  ["$kpaI__kaiqi1"] = "凿山开险，破蜀建功！",
  ["$kpaI__kaiqi2"] = "明战于剑阁，暗渡于阴平！",
}

return kaiqi
